package com.tianze.library.base;

import android.view.View;

import com.tianze.library.R;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;

import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.header.StoreHouseHeader;
import in.srain.cube.views.ptr.indicator.PtrIndicator;
import in.srain.cube.views.ptr.util.PtrLocalDisplay;

public abstract class BaseStoreHouseRefreshRecyclerFragment extends BaseRefreshFragment implements RecyclerBaseAdapter.OnItemClickListener, RecyclerBaseAdapter.OnItemLongClickListener {

    protected PtrFrameLayout frame;

    @Override
    protected int getLayoutId() {
        return R.layout.tiza_fragment_recycler_store_house_refresh;
    }

    @Override
    protected void beforeInitView() {
        frame = ButterKnife.findById(rootView, R.id.ptrFrameLayout);
        StoreHouseHeader header = new StoreHouseHeader(getActivity());
        header.setTextColor(0x9A9A9A);
        header.setPadding(0, PtrLocalDisplay.dp2px(15), 0, 0);
        header.initWithString(getString(R.string.refresh_header));
        frame.addPtrUIHandler(
                new PtrUIHandler() {

                    @Override
                    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
                    }

                    @Override
                    public void onUIRefreshPrepare(PtrFrameLayout frame) {

                    }

                    @Override
                    public void onUIRefreshBegin(PtrFrameLayout frame) {

                    }

                    @Override
                    public void onUIRefreshComplete(PtrFrameLayout frame) {

                    }

                    @Override
                    public void onUIReset(PtrFrameLayout frame) {

                    }

                }
        );

        frame.setDurationToCloseHeader(1500);
        frame.setHeaderView(header);
        frame.addPtrUIHandler(header);
        frame.postDelayed(
                new Runnable() { //进入自动延迟刷新
                    @Override
                    public void run() {
                        frame.autoRefresh(true);
                    }
                }, 300
        );

        frame.setPtrHandler(
                new PtrHandler() {
                    @Override
                    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                    }

                    @Override
                    public void onRefreshBegin(final PtrFrameLayout frame) {
                        refresh();
                    }
                }
        );
    }

    @Override
    protected void onRequestFinish() {
        super.onRequestFinish();
        frame.refreshComplete();
    }

    /**
     * @return false:自动刷新
     */
    protected boolean requestDataIfViewCreated() {
        return false;
    }
}
